<?php

namespace App\Http\Controllers\API;

use App\Http\Controllers\BaseController;
use App\Models\ExhibitorInformation;
use App\Models\JournalContent;
use App\Models\MainBusiness;
use App\Models\MessageProcessCount;
use App\Services\PublicService;
use Illuminate\Http\Request;

class JournalContentController extends BaseController
{
    public function add(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_message_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $message_process_count = MessageProcessCount::query()
            ->where('model_type_id', 2)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->first();

        if ($message_process_count) {
            return $this->failure('已完成');
        }

        $data = $request->all();
        $cn_enterprise_full = $data['cn_enterprise_full'];
        $cn_enterprise_short = $data['cn_enterprise_short'] ?? '';
        $en_enterprise_full = $data['en_enterprise_full'] ?? '';
        $cn_address = $data['cn_address'];
        $post_code = $data['post_code'] ?? '';
        $telephone = $data['telephone'];
        $wx = $data['wx'] ?? '';
        $e_mail = $data['e_mail'];
        $data['exhibitor_information_id'] = session('exhibitor_information_id');
//        $data['main_business'] = json_encode($data['main_business']);
        $journal_content = JournalContent::query()->where('exhibitor_information_id', $exhibitor_information_id)->first();
//        dd($data);
        if ($journal_content) {
            $message_process_count->update($data);
        } else {
            $journal_content = JournalContent::query()->create($data);
        }

        $exhibitor_information = ExhibitorInformation::query()->find($exhibitor_information_id);

        if ($exhibitor_information) {
            $exhibitor_information_update_data['en_name'] = $cn_enterprise_full;
            $exhibitor_information_update_data['cn_name'] = $cn_enterprise_short;
            $exhibitor_information_update_data['en_name_full'] = $en_enterprise_full;
            $exhibitor_information_update_data['address'] = $cn_address;
            $exhibitor_information_update_data['post_code'] = $post_code;
            $exhibitor_information_update_data['telephone'] = $telephone;
            $exhibitor_information_update_data['wx'] = $wx;
            $exhibitor_information_update_data['e_mail'] = $e_mail;
            $exhibitor_information->update($exhibitor_information_update_data);
        }

        MessageProcessCount::query()->insert(
            ['model_type_id' => 2,//模块类型1，安全责任书，2：会刊内容提交，3：展商证件申请
                'model_id' => $journal_content->id,
                'certificates_exhibitor_information_id' => session('exhibitor_information_id'),
                'exhibitor_message_id' => session('exhibitor_message_id')
            ]
        );

        if ($journal_content) {
            return $this->success('成功');
        } else {
            return $this->failure('失败');
        }
    }

    public function update(Request $request)
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_message_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $data = $request->all();

        $data['exhibitor_information_id'] = session('exhibitor_information_id');
        $data['main_business'] = json_encode($data['main_business']);

        $message_process_count = MessageProcessCount::query()
            ->where('model_type_id', 2)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->first();

        if ($message_process_count) {
            $journal_content = JournalContent::query()
                ->where('id', $message_process_count->model_id)
                ->update($data);

            if ($journal_content) {
                return $this->success('成功');
            } else {
                return $this->failure('失败');
            }
        }

        return $this->failure('失败');
    }

    public function show()
    {
        $exhibitor_information_id = session()->get('exhibitor_information_id');
        $exhibitor_message_id = session()->get('exhibitor_message_id');

        if (!$exhibitor_message_id) {
            return $this->failure('数据错误，重新登录');
        }

        if (!$exhibitor_information_id) {
            return $this->failure('登录超时，重新登录');
        }

        $message_process_count = MessageProcessCount::query()->where('model_type_id', 2)
            ->where('certificates_exhibitor_information_id', $exhibitor_information_id)
            ->first();

        if ($message_process_count) {
            $journal_content = JournalContent::query()->find($message_process_count->model_id);
            $information = ExhibitorInformation::find($exhibitor_information_id);

            $main_business_arr = explode(',', $journal_content['main_business']);

            foreach ($main_business_arr as $key => $value) {
                $main_business = MainBusiness::find($value);
                $main_business_list[$key] = $main_business['name'];
            }

            $journal_content['main_business_arr'] = $main_business_arr;
            $journal_content['main_business_list'] = implode(',', $main_business_list);
            $journal_content['booth_number'] = $information->booth_number;

            return $this->success($journal_content ?? '');
        } else {
            $information = ExhibitorInformation::find($exhibitor_information_id);
            $information['cn_enterprise_full'] = $information['name'];
            $information['en_enterprise_full'] = $information['en_name_full'];
            $information['telephone'] = $information['phone'];
            $cn_company_introduction = '';
            if ($information->journal_content) {
                $cn_company_introduction = $information->journal_content->cn_company_introduction;
            }
            $information['cn_company_introduction'] = $cn_company_introduction;

            $en_company_introduction = '';
            if ($information->journal_content) {
                $en_company_introduction = $information->journal_content->en_company_introduction;
            }
            $information['en_company_introduction'] = $en_company_introduction;

            return $this->success($information ?? '');
        }
    }

}
